package org.xurui.sxl.protocol.command.impl;

import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.codec.FromDeviceMessageContext;
import org.jetlinks.core.message.event.EventMessage;
import org.xurui.sxl.protocol.command.CmdCodec;
import org.xurui.sxl.protocol.enums.CommandEnum;
import org.xurui.sxl.protocol.utils.NumberUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WIFI 到校/离校状态
 */
public class CmdCodec_SCHOOLSTATUS implements CmdCodec {
    @Override
    public boolean supportsCmd(String cmd) {
        return CommandEnum.SCHOOLSTATUS.name().equals(cmd);
    }

    @Override
    public boolean supportsRelayCmd() {
        return false;
    }

    @Override
    public List<DeviceMessage> parsePayload(String payload, FromDeviceMessageContext context) {
        //WIFI 到校/离校状态 设备上报 TODO 没有 SV,BAT
        //格式： *XX,YYYYYYYYYYYYYYY,SCHOOLSTATUS,HHMMSS,FLAG,NUM,MAC,RSSI,MAC,RSSI,,,DDMMYY,FFFFFFFF#
        //示例1：*TE,7893267561,SCHOOLSTATUS,130305,1,1,B4:0B:44:CA:D4:8A,-90,220902,FFFFFFFF#
        //示例2：*TE,7893267561,SCHOOLSTATUS,130305,0,2,B3:0B:44:CA:D4:8A,-90,B3:0B:44:CA:D4:8A,-90,220902,FFFFFFFF#
        //回复格式：*XX,YYYYYYYYYYYYYYY,SCHOOLSTATUS,HHMMSS,FLAG#
        //回复示例：*TE,7893267561,SCHOOLSTATUS,130305,1# //在校
        //        *TE,7893267561,SCHOOLSTATUS,130305,0# //离校
        //参数说明：FLAG 0：离校；1：在校
        //参数说明：NUM 数量，1-6，最多 6 个
        //参数说明：RSSI 接收信号，负值，如：-90
        //解析数据为事件上报
        EventMessage eventMessage = new EventMessage();
        //ReportPropertyMessage report = ReportPropertyMessage.create();
        String[] payloadArr = payload.replaceAll("\\*","").split(",");
        String deviceId = payloadArr[1];
        eventMessage.setEvent(CommandEnum.SCHOOLSTATUS.name());
        eventMessage.setDeviceId(deviceId);
        eventMessage.setMessageId(deviceId+"_"+payloadArr[3]);
        eventMessage.setTimestamp(System.currentTimeMillis());
        Map<String, Object> properties = new HashMap<>();
        properties.put("uptime",payloadArr[3]);
        properties.put("flag", NumberUtils.parseInt(payloadArr[4]));
        properties.put("num",NumberUtils.parseInt(payloadArr[5]));
        int macNumber = NumberUtils.parseInt(payloadArr[6]);//数量，1-6，最多 6 个
        List<Map> macs = new ArrayList<>();
        for(int i=0;i<macNumber;i++){
            Map mac = new HashMap();
            mac.put("mac",payloadArr[7+i*2]);
            mac.put("rssi",payloadArr[8+i*2]);
            macs.add(mac);
        }
        properties.put("macinfo",macs);
        properties.put("upday",payloadArr[7+macNumber*2]);

        String trackerStatusHex = payloadArr[7+macNumber*2+1];
        Map<String,Object> trackerStatusMap = parseTrackerStatus(trackerStatusHex);
        properties.put("trackerstatus",trackerStatusMap);
        //report.setProperties(properties);
        eventMessage.setData(properties);
        //回复设备数据
        //TODO 暂时没找到设备回复消息的方法
        //TODO 没有回复参数  FLAG
//        String day= BinaryUtils.getDateFormat();
//        String reply="*"+payloadArr[0]+","+deviceId+",V4,"+CommandEnum.SCHOOLSTATUS.name()+","+day+","+payloadArr[4];
//        context.getSession()
//            .send(EncodedMessage
//                .simple(Unpooled.wrappedBuffer(reply.getBytes())))
//            .subscribe();
        //return genDeviceMessage(report);
        return genDeviceMessage(eventMessage);
    }
}
